//
//  NSString+CQString.m
//  CQUtils
//
//  Created by runo on 16/5/17.
//  Copyright © 2016年 com.runo. All rights reserved.
//

#import "NSString+CQString.h"
#import <CommonCrypto/CommonDigest.h>
#import "CQCommonHeader.h"

@implementation NSString (CQString)

/************类方法****************/

+(BOOL)cqIsPhoneNumber{
    
    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL isMatch = [pred evaluateWithObject:self];
    
    return isMatch;
}


/************序列化字符串****************/

-(NSString *)cqTrim{
    NSString * tempStr=[self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
    return tempStr;
}

-(NSString *)cqTrimLeft{
    NSInteger length = [self length];
    NSInteger loc;
    for (NSInteger location=0; location < length; location++) {
        int tempChar=[self characterAtIndex:location];
        if (!isspace(tempChar)) {
            loc=location;
            break;
        }
    }
    return [self substringWithRange:NSMakeRange(loc, length - loc)];
}

-(NSString *)cqTrimRight{
    
    NSInteger length=[self length];
    NSInteger loc;
    for (NSInteger location=length-1; location>=0; location--) {
        int tempChar=[self characterAtIndex:location];
        if (!isspace(tempChar)) {
            loc=location;
            break;
        }
    }
    return [self substringToIndex:loc];
}

-(NSString *)cqGetMD5{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr,strlen(cStr),result);
    NSString *md5Str = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",result[0],result[1],result[2],result[3],result[4],result[5],result[6],result[7],result[8],result[9],result[10],result[11],result[12],result[13],result[14],result[15]];
    return md5Str;
}

-(CGSize)cqDefaultSize{
    //计算文字高度
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize LabeLsize = [self boundingRectWithSize:CGSizeMake(kScreenWidth-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return LabeLsize;
}

-(CGSize)cqStringSize:(CGSize)maxSize FontSize:(CGFloat)fontSize{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    CGSize LabeLsize = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return LabeLsize;
}

@end
